package biz.datalk.industrialland.admin.modules.system.controller;

import biz.datalk.commons.utils.BasicParserUtils;
import biz.datalk.commons.utils.StringUtils;
import biz.datalk.industrialland.admin.modules.system.pojo.dto.SysUserDTO;
import biz.datalk.industrialland.admin.modules.system.pojo.vo.SysUserVO;
import biz.datalk.industrialland.admin.modules.system.service.ISysUserService;
import biz.datalk.industrialland.common.encrypt.annotation.RSAES;
import biz.datalk.industrialland.common.json.annotation.Json;
import biz.datalk.industrialland.common.log.aspect.annotation.SysLog;
import biz.datalk.industrialland.common.log.def.OptEnum;
import biz.datalk.industrialland.common.result.UnifyResult;
import biz.datalk.industrialland.common.util.I18NUtil;
import biz.datalk.industrialland.common.validated.Delete;
import biz.datalk.industrialland.common.validated.Insert;
import biz.datalk.industrialland.common.validated.Update;
import biz.datalk.industrialland.mbg.pojo.po.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tarofang@163.com
 * @date 2019年08月12日
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    private final ISysUserService sysUserService;

    @Autowired
    public SysUserController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @RSAES
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysUser.class, include = "", filter = "password,salt,createTime,updateTime")
    @SysLog(OptEnum.LAND_USER_10201)
    public UnifyResult list(@RequestBody SysUserDTO sysUserDTO) {
        return sysUserService.list(sysUserDTO);
    }

    @RSAES
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_USER_10202)
    public UnifyResult addUser(@RequestBody @Validated({Insert.class}) SysUserDTO sysUserDTO) {
        return sysUserService.addUser(sysUserDTO);
    }

    @RSAES
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_USER_10203)
    public UnifyResult updateUser(@RequestBody @Validated({Update.class}) SysUserDTO sysUserDTO) {
        return sysUserService.updateUser(sysUserDTO);
    }

    @RSAES
    @PostMapping(value = "/changeStatus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_USER_10204)
    public UnifyResult changeStatus(@RequestBody SysUserDTO sysUserDTO) {
        sysUserService.changeStatus(sysUserDTO);
        return UnifyResult.success();
    }

    @RSAES
    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Json(type = SysUserVO.class, include = "", filter = "password,salt,createTime,updateTime")
    @SysLog(OptEnum.LAND_USER_10205)
    public UnifyResult detail(@RequestBody SysUserDTO sysUserDTO) {
        Long id = sysUserDTO.getId();
        if (id == null || id <= 0L) {
            return UnifyResult.fail(I18NUtil.getMsg("user.no.exists.or.delete"));
        }
        SysUserVO sysUserVO = sysUserService.detailById(id);
        return UnifyResult.success().setData(sysUserVO);
    }

    @RSAES
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_USER_10206)
    public UnifyResult delete(@RequestBody @Validated({Delete.class}) SysUserDTO sysUserDTO) {
        String idStr = sysUserDTO.getSysids();
        if (StringUtils.isBlank(idStr)) {
            return UnifyResult.fail(I18NUtil.getMsg("user.no.exists.or.delete"));
        }
        Long id = BasicParserUtils.parseLong(idStr, 0L);
        if (id <= 0L) {
            return UnifyResult.fail(I18NUtil.getMsg("user.no.exists.or.delete"));
        }
        sysUserService.deleteById(id);
        return UnifyResult.success();
    }

    @RSAES
    @PostMapping(value = "/changePwd", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(OptEnum.LAND_USER_10207)
    public UnifyResult changePwd(@RequestBody SysUserDTO sysUserDTO) {
        sysUserService.changePwd(sysUserDTO);
        return UnifyResult.success();
    }
}
